import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueJsx from '@vitejs/plugin-vue-jsx'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
	const env = loadEnv(mode, process.cwd())
	const { VITE_APP_ENV, VITE_APP_BASE,VITE_APP_BASE_API } = env

	return {
		plugins: [
			vue(),
			AutoImport({
				resolvers: [ElementPlusResolver()]
			}),
			Components({
				resolvers: [ElementPlusResolver()]
			}),
			vueJsx()
		],
		base: VITE_APP_ENV === 'production' ? '/huizhi' : '/', // router同步更新
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url))
			}
		},
		server: {
			host: '0.0.0.0',
			port: 8080,
			open: true,
			cors: true,
			// Load proxy configuration from .env.development
			proxy: {
				// '/dev-api': {
				// 	target: 'http://43.136.34.132:8089',
				// 	changeOrigin: true,
				// 	rewrite: p => p.replace(/^\/dev-api/, '')
				// },
				// '/prod-api': {
				// 	target: 'http://43.136.34.132:8082',
				// 	changeOrigin: true,
				// 	rewrite: p => p.replace(/^\/prod-api/, '')
				// }
				[VITE_APP_BASE_API]: {
					target: VITE_APP_BASE,
					changeOrigin: true,
					rewrite: p => p.replace(new RegExp('^'+ VITE_APP_BASE_API), '')
				}
			}
		},
		build: {
			outDir: 'dist',
			minify: 'esbuild',
			// esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
			// minify: "terser",
			// terserOptions: {
			// 	compress: {
			// 		drop_console: viteEnv.VITE_DROP_CONSOLE,
			// 		drop_debugger: true
			// 	}
			// },
			// 禁用 gzip 压缩大小报告，可略微减少打包时间
			reportCompressedSize: false,
			// 规定触发警告的 chunk 大小
			chunkSizeWarningLimit: 2000,
			rollupOptions: {
				output: {
					// Static resource classification and packaging
					chunkFileNames: 'assets/js/[name]-[hash].js',
					entryFileNames: 'assets/js/[name]-[hash].js',
					assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
				}
			}
		}
	}
})
